package com.example.springai.config;

import com.example.springai.bean.SystemConstants;
import com.example.springai.repository.InDatabaseChatMemory;
import com.example.springai.tool.CourseTools;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AiConfig {



    @Bean
    public ChatMemory chatMemory(){
        return new InMemoryChatMemory();
    }


    @Autowired
    private InDatabaseChatMemory inDatabaseChatMemory;


    // AI 对话
    @Bean
    public ChatClient chatClient(OpenAiChatModel aiChatModel) {
        return ChatClient.builder(aiChatModel)
                .defaultOptions(ChatOptions.builder().model("Qwen/Qwen2.5-VL-32B-Instruct").build())
                // 设置默认角色 方法中加入角色的提示词
                .defaultSystem(SystemConstants.AI_CHAT_PROMPT)
                .defaultAdvisors(new SimpleLoggerAdvisor(), new MessageChatMemoryAdvisor(inDatabaseChatMemory)) //配置日志 Advisor
                .build();
    }


    // 哄哄模拟器
    @Bean
    public ChatClient gameChatClient(OpenAiChatModel aiChatModel) {
        return ChatClient.builder(aiChatModel)
                /// 设置默认角色, 方法中加入角色的提示词
                .defaultOptions(ChatOptions.builder().model("deepseek-ai/DeepSeek-V3").build())
                .defaultSystem(SystemConstants.GAME_SYSTEM_PROMPT)
                .defaultAdvisors(new SimpleLoggerAdvisor(), new MessageChatMemoryAdvisor(chatMemory()))
                .build();
    }


    // 智能客服
    @Bean
    public ChatClient serviceChatClient(OpenAiChatModel aiChatModel, CourseTools courseTools) {
        return ChatClient.builder(aiChatModel)
                .defaultSystem(SystemConstants.CUSTOMER_SERVICE_SYSTEM)
                .defaultTools(courseTools)
                .defaultAdvisors(new SimpleLoggerAdvisor(), new MessageChatMemoryAdvisor(inDatabaseChatMemory))
                .build();
    }



}
